package edu.ciee.tz.dao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import edu.ciee.tz.model.Yuangong;


@Component("yuangongDao")
public class YuangongDAO {

	public List<Yuangong> search(Yuangong yg) throws Exception {

		String query = "where 1=1";
			
		if (yg.getId() != 0) 
			query = query + " and id like '%" + yg.getId() + "%'";// this.getID()
		if (yg.getName() != null) 
			query = query + " and name like '%" + yg.getName() + "%'"; // this.getName()
		if (yg.getSex() != null) 
			query = query + " and sex like '%" + yg.getSex() + "%'"; // this.getName()
		if (yg.getBirthday()!= null) 
			query = query + " and birthday like '%" + yg.getBirthday()+ "%'"; // this.getName()
		if (yg.getTele()!= null) 
			query = query + " and tele like '%" + yg.getTele()+ "%'"; // this.getName()
		System.out.println(query);
		
		if (yg.getZhibie().getId()!= 0) 
			query = query + " and zhibie like '%" + yg.getZhibie().getId()+ "%'"; // this.getName()
		
		System.out.println(query);
		
		@SuppressWarnings("unchecked")
		List<Yuangong> yg_list = hibernateTemplate.find("from Yuangong u "+ query);

		return yg_list;
	}
	public List<Yuangong> searchAll() throws Exception {
		@SuppressWarnings("unchecked")
		List<Yuangong> yg_list = hibernateTemplate.find("from Yuangong u ");
		return yg_list;
	}
	public void insert(Yuangong yg) throws Exception {
		System.out.println("zhibie:"+yg.getZhibie().getId());

		this.getHibernateTemplate().save(yg);
	}

	public void delete(Yuangong yg) throws Exception {
		this.getHibernateTemplate().delete(yg);

	}

	public void update(Yuangong yg) throws Exception {
		this.getHibernateTemplate().update(yg);
	}

	private HibernateTemplate hibernateTemplate;

	public HibernateTemplate getHibernateTemplate() {
		return hibernateTemplate;
	}

	@Resource
	public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
		this.hibernateTemplate = hibernateTemplate;
	}
}
